﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using DDD_AOP_WCF.Domain.DTO;
using DDD_AOP_WCF.Domain.Products;
using DDD_AOP_WCF.ServiceContracts;
using Infrastructure;
using Project.Unity;
using Domain.Core.Specification;
namespace 学习陈晴阳的DDD_AOP_WCF.Controllers
{
    using DDD_AOP_WCF.Domain;
    using Domain.Core.Repository;
    using Infrastructure.Core;
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        public List<Product> GetProductBySpecification(ISpecification<Product> specification)
        {
            var _productRepository = ServiceLocator.Instance.GetService<IProductRepository>();
            return _productRepository.GetModel(specification).ToList();
        }

        public ActionResult Index(string name)
        {

            #region Unity测试
            //直接调用Unity
            // IRepository<User> traceManager = IoCFactory.Instance.CurrentContainer.Resolve<IRepository<User>>();
            //通过serviceLocator调用Unity
            //IProductService productService = ServiceLocator.Instance.GetService<IProductService>();
            #endregion

            List<DDD_AOP_WCF.Domain.DTO.ProductDTO> model = new List<ProductDTO>();

            //通过WCF获取远程数据
            using (ServiceProxy<IProductService> proxy = new ServiceProxy<IProductService>())
            {
                if (string.IsNullOrWhiteSpace(name))
                    model = proxy.Channel.GetProduct();
                else
                    model = proxy.Channel.GetProduct().Where(i => i.ProductName.Contains(name)).ToList();
            }
            if (Request.IsAjaxRequest())
                return PartialView("List", model);
            return View(model);
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(ProductDTO entity)
        {
            entity.CreateDate = DateTime.Now;
            entity.SaleCount = 0;
            entity.ClickCount = 0;

            using (ServiceProxy<IProductService> proxy = new ServiceProxy<IProductService>())
            {
                proxy.Channel.CreateProduct(entity);
            }
            return RedirectToAction("Index");
        }

        public ActionResult Delete(int id)
        {
            using (ServiceProxy<IProductService> proxy = new ServiceProxy<IProductService>())
            {
                proxy.Channel.DelProduct(id);
            }
            return RedirectToAction("Index");
        }

        public ActionResult Edit(int id)
        {
            ProductDTO product = new ProductDTO();
            using (ServiceProxy<IProductService> proxy = new ServiceProxy<IProductService>())
            {
                product = proxy.Channel.GetProductByID(id);
            }
            return View(product);
        }

        [HttpPost]
        public ActionResult Edit(ProductDTO entity)
        {
            using (ServiceProxy<IProductService> proxy = new ServiceProxy<IProductService>())
            {
                proxy.Channel.ModifyProduct(entity);
            }
            return RedirectToAction("Index");
        }
    }
}
